import OSS from 'ali-oss';
import { isTrue } from './tools';
// import { request as getGetassume } from '@/api/server/mods/store/getGetassumeRole'
export interface XYFile extends File {
	uid?: string | number
}
export interface OssData {
	filename?: string
	name?: string
	url?: string
	byte?: string
	type?: string
}
/** https://help.aliyun.com/zh/oss/developer-reference/initialization-10?spm=a2c4g.11186623.0.0.50244c204fDa3C */
export interface OssPolicyDto {
	accessKey: string
	policy: string
	signature: string
	path: string
	host: string
	expirationDateTime: string
	minSize: number
	maxSize: number
}
export const OSSURL = 'https://oss.jiajuy.com/ysj'
class aliOss {
	private endpoint = 'oss.jiajuy.com'
	private accessKeyId = null as null | string
	private accessKeySecret = null as null | string
	private bucket = 'shejiy'
	private timeout = 5 * 60 * 1000
	private client: OSS | undefined
	private durationSeconds = 0
	private stsToken = null as null | string
	private expirationDateTime = new Date()
	constructor() {
		// this.init()
	}
	async init() {
		// const { success, data } = await getGetassume()
		// if (success) {
		// 	this.expirationDateTime = new Date()
		// 	/** 设置过期时间 */
		// 	this.expirationDateTime.setSeconds(this.expirationDateTime.getSeconds() + 2000);
		// 	this.accessKeyId = data.AccessKeyId
		// 	this.accessKeySecret = data.AccessKeySecret
		// 	this.stsToken = data.SecurityToken
		// 	this.durationSeconds = 2000
		// 	this.client = new OSS({
		// 		cname: true,
		// 		endpoint: this.endpoint,
		// 		accessKeyId: data.AccessKeyId,
		// 		accessKeySecret: data.AccessKeySecret,
		// 		bucket: this.bucket,
		// 		timeout: this.timeout,
		// 		stsToken: data.SecurityToken,
		// 		refreshSTSToken: async () => {
		// 			return {
		// 				accessKeyId: data.AccessKeyId as string,
		// 				accessKeySecret: data.AccessKeySecret as string,
		// 				stsToken: data.SecurityToken as string,
		// 			}
		// 		}
		// 	});
		// }

	}
	/**
	 * 普通上传
	 * @param file 上传文件
	 * @param folder 上传文件夹路径
	 * @returns 
	 */
	uploadOSS(file: XYFile, folder?: string): Promise<OssData> {
		return new Promise(async (resolve, reject) => {
			try {
				console.log(this.expirationDateTime);

				// 判断当前时间是否大于过期时间
				if (new Date().getTime() >= this.expirationDateTime.getTime() || !isTrue(this.accessKeyId)) {
					await this.init()
				}
				const extension = file.name.split('.').pop()?.toLowerCase();
				const result = await (this.client as OSS).put(isTrue(folder) ? `${folder}/${file.uid}.${extension}` : `${file.uid}.${extension}`, file);
				resolve({
					filename: result.name,
					url: result.url,
				} as OssData)
			} catch (error: any) {
				reject(error.message)
				console.log(error.message);
			}
		})
	}
	async removeOSS(filename: string) {
		try {
			// 填写Object完整路径。Object完整路径中不能包含Bucket名称。
			const result = await this.client?.delete(filename);
			return {
				success: true,
				message: '删除成功'
			}
		} catch (error) {
			console.log(error);
			return {
				success: false,
				message: error
			}
		}
	}
}
export const AilOSS = new aliOss()
